<template>
  <div class="container">
    <div class="box">
      <h1>{{ $t(systemTitle) }}</h1>
      <el-form class="form">
        <el-input
          size="large"
          v-model="form.name"
          :placeholder="$t('message.system.userName')"
          type="text"
          maxlength="50"
        >
          <template #prepend>
            <i class="sfont system-xingmingyonghumingnicheng"></i>
          </template>
        </el-input>
        <el-input
          size="large"
          ref="password"
          v-model="form.password"
          :type="passwordType"
          class="marginT12"
          :placeholder="$t('message.system.password')"
          name="password"
          maxlength="50"
        >
          <template #prepend>
            <i class="sfont system-mima"></i>
          </template>
          <template #append>
            <i
              class="sfont password-icon"
              :class="passwordType ? 'system-yanjing-guan' : 'system-yanjing'"
              @click="passwordTypeChange"
            ></i>
          </template>
        </el-input>
        <el-button
          size="large"
          type="primary"
          @click="submit"
          class="marginT40"
          :loading="loading"
          style="
            width: 100%;
            letter-spacing: 4px;
            background-image: linear-gradient(
              to right,
              #0d6fde 0%,
              #1245d4 100%
            );
          "
          >{{ $t("message.system.login") }}
        </el-button>
      </el-form>
      <div class="fixed-top-right">
        <!--        <select-lang />-->
      </div>
    </div>
  </div>
</template>

<script>
import { systemTitle } from "@/config";
import { defineComponent, ref, reactive } from "vue";
import { useStore } from "vuex";
import { useRouter, useRoute } from "vue-router";
import { addRoutes } from "@/router";
import { ElMessage } from "element-plus";
import selectLang from "@/layout/Header/functionList/word.vue";
// import service from "@/utils/system/request";

import { useI18n } from "vue-i18n";

export default defineComponent({
  components: {
    // selectLang
  },
  setup() {
    const { t } = useI18n();
    const store = useStore();
    const loading = ref(false);
    const router = useRouter();
    const route = useRoute();
    const form = reactive({
      name: "",
      password: ""
    });
    const passwordType = ref("password");
    const passwordTypeChange = () => {
      passwordType.value === ""
        ? (passwordType.value = "password")
        : (passwordType.value = "");
    };
    const checkForm = () => {
      loading.value = true;
      return new Promise(resolve => {
        if (form.name === "") {
          ElMessage.warning({
            message: t("key972"),
            type: "warning"
          });
          return;
        }
        if (form.password === "") {
          ElMessage.warning({
            message: t("key973"),
            type: "warning"
          });
          return;
        }
        resolve(true);
      });
    };
    const submit = () => {
      checkForm().then(() => {
        let params = {
          mobile: form.name,
          password: form.password
        };

        // service.post('admin/login',params).then(({code,data})=>{
        //   console.log(store)
        //   console.log(data.token,'12222')
        //   store.commit('getInfo', { token: data.token })
        //   console.log(code,data)
        //   loading.value = true
        //   ElMessage.success({
        //     message: '登录成功',
        //     type: 'success',
        //     showClose: true,
        //     duration: 1000store
        //   })
        // setTimeout(()=>{
        // addRoutes()
        // router.push(route.query.redirect || '/')
        // },500)
        // })
        store
          .dispatch("user/login", params)
          .then(res => {
            console.log(res);
            // debugger;
            if (res.code === 0) {
              loading.value = false;
              ElMessage.success({
                message: "login Success",
                type: "success",
                showClose: true,
                duration: 1000
              });
              setTimeout(() => {
                addRoutes();
                // console.log("route.query.redirect", route);
                // return;
                router.push("/");
                // router.push(route.query.redirect || "/");
              }, 500);
            } else {
              loading.value = false;
            }
          })
          .catch(res => {});
        //
        // })
      });
    };
    return {
      systemTitle,
      form,
      passwordType,
      loading,
      passwordTypeChange,
      submit
    };
  }
});
</script>

<style lang="scss" scoped>
.container {
  position: relative;
  width: 100vw;
  height: 100vh;
  background-color: #ffffff;
  background-image: url("https://image.fulllinkai.com/202312/15/e70b05f5737a18b51a7d0c0989435611.png");
  background-image: url("https://myicellar-images.oss-cn-hongkong.aliyuncs.com/202406/25/84c64078563748f00f5b16bb5ac2f550.jpeg");
  background-image: url("https://myicellar-images.oss-cn-hongkong.aliyuncs.com/202406/25/511a4f144610fe46bda937af5bc32162.jpeg");
  background-size: cover;
  background-repeat: no-repeat;

  .box {
    width: 500px;
    position: absolute;
    left: 50%;
    top: 50%;
    background: white;
    border-radius: 8px;
    transform: translate(-50%, -50%);
    height: 440px;
    overflow: hidden;
    box-shadow:
      0 6px 20px 5px rgba(152, 152, 152, 0.1),
      0 16px 24px 2px rgba(117, 117, 117, 0.14);

    h1 {
      margin-top: 72px;
      text-align: center;
    }

    .form {
      width: 80%;
      margin: 50px auto 15px;

      .el-input {
        margin-bottom: 20px;
      }

      .password-icon {
        cursor: pointer;
        color: #7e9cff;
      }
    }

    .fixed-top-right {
      position: absolute;
      top: 10px;
      right: 10px;
    }
  }
}

@media screen and (max-width: 750px) {
  .container .box {
    width: 100vw;
    height: 100vh;
    box-shadow: none;
    left: 0;
    top: 0;
    transform: none;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    h1 {
      margin-top: 0;
    }

    .form {
    }
  }
}
</style>
